package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishService dishService;

    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        //先查询
        //构造条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Setmeal::getName, name);
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        page = setmealMapper.selectPage(page, qw);
        //遍历
        List<Setmeal> setmealList = page.getRecords();
        for (Setmeal setmeal : setmealList) {
            //按照套餐id查询出套餐菜品
            LambdaQueryWrapper<SetmealDish> sqw = new LambdaQueryWrapper<>();
            sqw.eq(SetmealDish::getDishId, setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(sqw);
            //放入数据库
            setmeal.setSetmealDishes(setmealDishList);
            //根据category_id 查询并封装分类名称
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            setmeal.setCategoryName(category.getName());
        }
        return page;
    }

    @Override
    public void save(Setmeal setmeal) {
        //添加基本信息
        setmealMapper.insert(setmeal);
        //遍历菜品
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public Setmeal findById(Long id) {
        //根据id查询基本信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //根据id查询菜品信息
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);
        //放入对象
        setmeal.setSetmealDishes(setmealDishes);
        return setmeal;
    }

    @Override
    public void update(Setmeal setmeal) {
        //正常修改
        setmealMapper.updateById(setmeal);
        //根据id查询套餐菜品并删除
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(qw);
        //重新添加
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //关联id
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    //起售,停售
    @Override
    public void updateStatusSetmeal(Integer status, List<Long> ids) {
        //构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids);

        //构建实体类
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);

        //修改状态
        setmealMapper.update(setmeal, qw);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids);
        qw.eq(Setmeal::getStatus, 1);
        Integer count = setmealMapper.selectCount(qw);
        if (count > 0) {
            throw new CustomException("套餐中有启售状态，请先停售再删除~~~");
        }
        //删除套餐
        setmealMapper.deleteBatchIds(ids);
        //删除菜品
        LambdaQueryWrapper<SetmealDish> dqw = new LambdaQueryWrapper<>();
        dqw.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(dqw);

    }

    // 根据分类id+状态查询套餐列表
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        // 1.构建条件   select * from setmeal where category_id = ? and status = ?
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId, categoryId);
        qw.eq(Setmeal::getStatus, status);
        // 2.查询列表
        List<Setmeal> setmealList = setmealMapper.selectList(qw);
        // 3.返回结果
        return setmealList;
    }

    @Override
    public List<Dish> findDishDetail(Long id) {
        // 1.查询套餐菜品中间表集合
        // 1-1 构建条件  select * from setmeal_dish where setmeal_id = ?
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(qw);
        // 2.遍历
        List<Dish> dishList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) {
            // 2-1 查询菜品
            Dish dish = dishService.findByid(setmealDish.getDishId());
            // 2-2 添加套餐中数量
            dish.setCopies(setmealDish.getCopies());
            // 2-3 添加返回菜品集合
            dishList.add(dish);
        }
        return dishList;
    }
}